/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-06-10     31603       the first version
 */

#include <rtthread.h>
#include <rtdevice.h>

#define UART_NAME       "uart3"    // USART3设备名称（根据BSP可能不同）
#define BAUD_RATE       115200     // ESP8266默认波特率

//#define AT_CMD_CWMODE   "AT+CWMODE=1\r\n"     // STA模式
//#define AT_CMD_CWJAP    "AT+CWJAP=\"SSID\",\"PASSWORD\"\r\n" // 替换为你的WiFi信息

// 串口设备句柄
static rt_device_t serial;

void esp8266_init(void) {
    // 查找串口设备
    serial = rt_device_find(UART_NAME);
    if (!serial) {
        rt_kprintf("find %s failed!\n", UART_NAME);
        return;
    }

    // 以读写及中断接收模式打开串口
    if (rt_device_open(serial, RT_DEVICE_FLAG_INT_RX) != RT_EOK) {
        rt_kprintf("open %s failed!\n", UART_NAME);
        return;
    }

    // 配置串口参数
    struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
    config.baud_rate = BAUD_RATE;
    config.data_bits = DATA_BITS_8;
    config.stop_bits = STOP_BITS_1;
    config.parity    = PARITY_NONE;
    rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);
}

void send_at_command(const char *cmd) {
    rt_size_t len = rt_strlen(cmd);
    rt_device_write(serial, 0, cmd, len);  // 发送AT指令
    rt_kprintf("Send: %s", cmd);           // 打印发送内容
}

















